﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using NLog;
using NLog.Targets;

namespace PortCommunicate.LogEntries
{
    [Target("WpfLogTarget")]
    public sealed class WpfLogTarget : TargetWithLayout
    {
        public ObservableCollection<LogEntry> LogEntries { get; } = new ObservableCollection<LogEntry>();
       

        protected override void Write(LogEventInfo logEvent)
        {
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                LogEntries.Add(new LogEntry
                {
                    TimeStamp = logEvent.TimeStamp,
                    Level = logEvent.Level.ToString(),
                    Logger = logEvent.LoggerName,
                    Message = logEvent.FormattedMessage,
                    Exception = logEvent.Exception?.ToString()
                });

            }));
        }
    }
}
